package com.jade.editor.anisotropy.parts;

import java.util.ArrayList;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

import com.jade.editor.anisotropy.node.Collector;
import com.jade.editor.anisotropy.node.Node;

import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.PaintEvent;

public class Preview extends Dialog {

	private int _xCount = 0;
	private int _yCount = 0;
	
	private int _xSize = 0;
	private int _ySize = 0;
	
	private Collector _cl = null;
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public Preview(Shell parentShell, int xCount, int yCount, int xSize, int ySize, Collector cl) {
		super(parentShell);
		_xCount = xCount;
		_yCount = yCount;
		
		_xSize = xSize;
		_ySize = ySize;
		
		_cl = cl;
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		ScrolledComposite scrolledComposite = new ScrolledComposite(container, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		scrolledComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));
		scrolledComposite.setMinSize(500, 700);
		
		
		Composite composite = new Composite(scrolledComposite, SWT.NONE);
		composite.setSize(_xSize * (_xCount + 1), _ySize * (_yCount + 1));
		composite.setLayout(null);
		scrolledComposite.setContent(composite);
		

		
		for (int i = 0, posx = 0; i <= _xCount; i++, posx += _xSize){
			for(int j = 0, posy = 0; j <= _yCount; j++, posy += _ySize){
				Composite drawsite = new Composite(composite, SWT.NONE);
				drawsite.addPaintListener(new PaintListener() {
					public void paintControl(PaintEvent e) {
						_cl.paint(e.gc);
						
						ArrayList<Node> nls = _cl.getNodeList();
						for(Node n : nls){
							e.gc.drawPolygon(poly(n.x, n.y));
						}
					}
				});
				drawsite.setBounds(posx, posy, _xSize, _ySize);
				drawsite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
			}
		}

		return container;
	}

	
	@Override
	protected boolean isResizable()  {
	  return true;
	}
	
	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		Button button = createButton(parent, IDialogConstants.CLOSE_ID, "Close\r\n",
				true);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				close();
			}
		});
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(703, 523);
	}
	
	
	private int[] poly(int x,int y){
		int poly[] = new int[8];
		int sp = 1;
		poly[0] = x - sp;
		poly[1] = y - sp;
		
		poly[2] = x + sp;
		poly[3] = y - sp;
		
		poly[4] = x + sp;
		poly[5] = y + sp;
		
		poly[6] = x - sp;
		poly[7] = y + sp;
		
		return poly;
	}

}
